package com.tensquare.service;

import antlr.StringUtils;
import com.tensquare.client.UserClient;
import com.tensquare.dao.ProblemDao;
import com.tensquare.dao.ProblemLabelDao;
import com.tensquare.entity.*;
import com.tensquare.util.IdWork;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ProblemService {
    @Autowired
    ProblemDao problemDao;
    @Autowired
    ProblemLabelDao problemLabelDao;
    @Autowired
    private IdWork idWork;

    @Autowired
    UserClient userClient;

    public List<Problem> findNewListByLabelId(String labelId, int page, int limit){
        if(limit == 0)
            limit = 10;
        PageResult pageResult =  new PageResult();
        pageResult.setCurrentPage(page);
        pageResult.setLimit(limit);
        return problemDao.findNewListByLabelId(labelId, pageResult);
    }

    public List<Problem> findHotListByLabelId(String labelId, int page, int limit){
        if(limit == 0)
            limit = 10;
        PageResult pageResult =  new PageResult();
        pageResult.setCurrentPage(page);
        pageResult.setLimit(limit);

        return problemDao.findHotListByLabelId(labelId, pageResult);
    }

    public List<Problem> findWaitListByLabelId(String labelId, int page, int limit){
        if(limit == 0)
            limit = 10;
        PageResult pageResult =  new PageResult();
        pageResult.setCurrentPage(page);
        pageResult.setLimit(limit);

        return problemDao.findWaitListByLabelId(labelId, pageResult);
    }

    @Transactional
    public boolean add(Map<String, String> map){
        //添加问题
        Problem problem = new Problem();
        if("".equals(map.get("title")) || map.get("title") == null
                || "".equals(map.get("content")) || map.get("content") == null ){
            return false;
        }

        problem.setTitle(map.get("title"));
        problem.setContent(map.get("content"));
        String uid = map.get("uid");
        User user = null;
        if ("".equals(uid) || uid == null){
            return false;
        }
        Result result = userClient.getById(uid);
        if (result.isFlag()){
            System.out.println(result);
            JSONObject json = new JSONObject();
            user = (User) result.getData();
            System.out.println(user);
//            String nickName = result.getData();
        }
        if (user == null)
            return false;

        System.err.println(user);
        String problemId = idWork.nextId();
        problem.setId(problemId);
        problem.setCreatetime(new Date());
        problem.setUserid(uid);
        problem.setNickname(user.getNickname());
        problemDao.add(problem);

        //添加标签与问题的关联
        if(map.get("tags") != null || !"".equals(map.get("tags"))){
            String [] tagsArr = map.get("tags").split(",");
            for(String tag : tagsArr){
                ProblemLabel pl = new ProblemLabel();
                //该id应该为label标签库中查出的id
                pl.setLabelid(idWork.nextId());
                pl.setProblemid(problemId);
                problemLabelDao.add(pl);
            }
        }

return true;
    }

}
